#!/bin/bash

date=`date +"%Y%m%d_%H%M"`
switch_list='./switch.txt'
switch_mac_diff="./switch_mac_diff_$date.txt"

cat /dev/null > $switch_mac_diff
wc -l $switch_listwhile read line

do
# 		echo $line
		if [ ! -z $line ]; then
			row_num=`echo $line | awk -F"," '{print $1}'`
			sw_ip=`echo $line | awk -F"," '{print $2}'`
			orig_mac=`echo $line | awk -F"," '{print $3}'`
			# echo "get [$sw_ip:$orig_mac]"
			#SNMPv2-SMI::mib-2.17.1.1.0
			corr_mac_raw=`/usr/bin/snmpwalk -v2c -c public $sw_ip SNMPv2-SMI::mib-2.17.1.1.0 | awk -F"=" '{print $2}' | awk -F":" '{print $2}'`
			# corr_mac_raw=`/usr/bin/snmpwalk -v2c -c public $sw_ip ipNetToMediaPhysAddress | grep $sw_ip | awk -F"=" '{print $2}' | awk '{print $2}'`
			# echo "$sw_ip - $orig_mac - $corr_mac_raw"
			if [ ! -z "$corr_mac_raw" ]; then
				# mac_sub1=`echo $corr_mac_raw | awk -F":" '{print 0$1}' | sed 's/.*\(..\)$/\1/'`
				# mac_sub2=`echo $corr_mac_raw | awk -F":" '{print 0$2}' | sed 's/.*\(..\)$/\1/'`
				# mac_sub3=`echo $corr_mac_raw | awk -F":" '{print 0$3}' | sed 's/.*\(..\)$/\1/'`
				# mac_sub4=`echo $corr_mac_raw | awk -F":" '{print 0$4}' | sed 's/.*\(..\)$/\1/'`
				# mac_sub5=`echo $corr_mac_raw | awk -F":" '{print 0$5}' | sed 's/.*\(..\)$/\1/'`
				# mac_sub6=`echo $corr_mac_raw | awk -F":" '{print 0$6}' | sed 's/.*\(..\)$/\1/'`
				
				# corr_mac="$mac_sub1$mac_sub2$mac_sub3$mac_sub4$mac_sub5$mac_sub6"
				corr_mac=`echo $corr_mac_raw | sed "s/ //g"`
				corr_mac=`echo $corr_mac | tr '[a-z]' '[A-Z]'`
				
				# echo "$sw_ip,$orig_mac,$corr_mac,-------"
				# echo $corr_mac
				if [ ! $corr_mac = $orig_mac ]; then
					echo "$row_num,$sw_ip,$orig_mac,$corr_mac"
					echo "$row_num,$sw_ip,$orig_mac,$corr_mac" >> $switch_mac_diff
				fi
			fi
		fi
done < $switch_list

echo "Done"